# -*- coding: utf-8 -*-
'''
Created on Apr 24, 2011

@author: Yang Sun
'''
import time
import math
from array import array

def decode(url, key): 
    url_buffer = url.decode('base64')
    key_buffer = key.encode('utf-8')
    result_buffer = ''
    i = 0
    urlLen = len(url_buffer)
    keyLen = len(key_buffer)
    while(i < urlLen):            
        result_buffer = result_buffer + chr(ord(url_buffer[i]) - ord(key_buffer[i % keyLen]))            
        i = i + 1
    return  result_buffer.encode('utf-8')
    
def encode(url, key):
    if(url is None or len(url) == 0):
        return ''

    url_buffer = url.decode('utf-8')
    key_buffer = key.decode('utf-8')
    result_buffer = ''
    i = 0
    urlLen = len(url_buffer)
    keyLen = len(key_buffer)
    while(i < urlLen):
        result_buffer = result_buffer + chr(ord(url_buffer[i]) + ord(key_buffer[i % keyLen]))
        i = i + 1
            
    return result_buffer.encode('base64')

def getKey(time_now):     
    s = 'qqqqqww'
    bytes = array('B', s.encode('utf-8'))
    destinationArray = [0] * 0x10
    num = 0
    while num < len(bytes):
        destinationArray[num] = bytes[num]
        num += 1
                            
    str2 = hex(time_now)
    sourceArray = array('B', str2[2:].encode('utf-8'))
    buffer4 = [0] * 0x10    
    num = 0
    while num < min(len(sourceArray), len(buffer4)):
        buffer4[num] = sourceArray[num]
        num += 1
                         
    tEncrypt(buffer4, destinationArray)
    return stringToHex(buffer4)
                
    
def getKeyFromStr(bs):
    buffer = [0] * 4
    i = 0
    while i < len(bs):
        buffer[i % 4] = (buffer[i % 4] ^ bs[i])
        i += 1
    return (((buffer[0] | (buffer[1] << 8)) | (buffer[2] << 0x10)) | (buffer[3] << 0x18))
    

def stringToHex(buffer):
    builder = array('B', '')
    i = 0
    while i < len(buffer):
        num2 = (buffer[i] & 15)
        num3 = ((buffer[i] >> 4) & 15)
        if (num2 > 9):
            builder.append(0x57 + num2)
        else:
            builder.append(0x30 + num2)   
        if (num3 > 9):
            builder.append(0x57 + num3)
        else:
            builder.append(0x30 + num3)                    
        i += 1
    return builder.tostring()

def tEncrypt(bytes, key):
    keyFromStr = getKeyFromStr(key)
    num2 = ((keyFromStr << 8) | (keyFromStr >> 0x18)) & 0xffffffff
    num3 = ((keyFromStr << 0x10) | (keyFromStr >> 0x10)) & 0xffffffff  
    num4 = ((keyFromStr << 0x18) | (keyFromStr >> 8)) & 0xffffffff
    i = 0
    while (i + 0x10) <= len(bytes):
        num6 = bytes[i] & 0xffffffff
        num7 = (bytes[i + 1] << 8) & 0xffffffff
        num8 = (bytes[i + 2] << 0x10) & 0xffffffff
        num9 = (bytes[i + 3] << 0x18) & 0xffffffff
        num10 = bytes[i + 4] & 0xffffffff
        num11 = (bytes[i + 5] << 8) & 0xffffffff
        num12 = (bytes[i + 6] << 0x10) & 0xffffffff
        num13 = (bytes[i + 7] << 0x18) & 0xffffffff
        num14 = (((num6 | num7) | num8) | num9) & 0xffffffff
        num15 = (((num10 | num11) | num12) | num13) & 0xffffffff
        num16 = 0
        j = 0
        while j < 0x20L:
            num16 += 0x9e3779b9
            num16 = num16 & 0xffffffff
            num14 += (((num15 << 4) + keyFromStr) ^ (num15 + num16)) ^ ((num15 >> 5) + num2)
            num14 = num14 & 0xffffffff
            num15 += (((num14 << 4) + num3) ^ (num14 + num16)) ^ ((num14 >> 5) + num4)
            num15 = num15 & 0xffffffff
            j += 1
        bytes[i] = (num14 & 0xff)
        bytes[i + 1] = ((num14 >> 8) & 0xff)
        bytes[i + 2] = ((num14 >> 0x10) & 0xff)
        bytes[i + 3] = ((num14 >> 0x18) & 0xff)
        bytes[i + 4] = (num15 & 0xff)
        bytes[i + 5] = ((num15 >> 8) & 0xff)
        bytes[i + 6] = ((num15 >> 0x10) & 0xff)
        bytes[i + 7] = ((num15 >> 0x18) & 0xff)
        i += 0x10 
        
'''
Sample Code
timestr = 'MonApr2515:33:582011UTC'                       
print getKey(timestr)
'''
